/* 
 * RequestParamViewer.java      1.00    2008-01-11
 *
 * Copyright (c) 2010 monolist. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of monolist.  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with monolist.
 */
package doit.framework.util;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : RequestParamViewer.java
 * description : RequestParamViewer
 * </pre> 
 * @version
 * <pre> 
 * 1.0  2008-01-11  monolist  최초 작성
 * 1.1  
 * </pre>
 */

@SuppressWarnings("unchecked")
public class RequestParamViewer {
    public static String getRequestLog( HttpServletRequest request) {
        return getRequestLog( "", request);
    }

    public static String getRequestLog( String logTitle, HttpServletRequest request) {
        StringBuffer sbMessage = new StringBuffer();

        sbMessage.append("[ Request ]").append("\n");
        sbMessage.append("  RemoteAddr    : ").append( request.getRemoteAddr()).append("\n");
        sbMessage.append("  ContextPath   : ").append( request.getContextPath()).append("\n");
        sbMessage.append("  RequestURI    : ").append( request.getRequestURI()).append("\n");
        sbMessage.append("  PathInfo      : ").append( request.getPathInfo()).append("\n");
        sbMessage.append("  ContentLength : ").append( request.getContentLength()).append("\n");
        sbMessage.append("  Method        : ").append( request.getMethod()).append("\n");
        sbMessage.append("  QueryString   : ").append(request.getQueryString()).append("\n");

        String szName;
        String[] arrValue;

        sbMessage.append("  [ Parameters ]").append("\n");
        Enumeration e = request.getParameterNames();

        while(e.hasMoreElements()) {
            szName = (String) e.nextElement();
            try {
                arrValue = request.getParameterValues(szName);
            }
            catch(Exception ee) {
                continue;
            }

            sbMessage.append("    ").append(szName).append(" : ");

            if(arrValue != null && arrValue.length > 0) {
                int nValue = arrValue.length;
                for(int i = 0; i < nValue; i++) {
                    sbMessage.append("[").append(arrValue[i]).append("]").append(i < nValue - 1 ? "," : "").append("\n");
                }
            }
        }

        Object objValue;

        sbMessage.append("  [ Attribute ]").append("\n");
        e = request.getAttributeNames();

        while(e.hasMoreElements()) {
            szName = (String) e.nextElement();
            objValue = request.getAttribute(szName);

            sbMessage.append("    ").append(szName).append(" : [");

            if(objValue != null) {
                sbMessage.append(((objValue instanceof String) ? (String) objValue : objValue.getClass().toString()));
            }
            sbMessage.append("]").append("\n");
        }

        sbMessage.append("------------------------------------------------------");

        return sbMessage.toString();
    }
}
